<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class ClassDate extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_class_date';



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();


        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-02-27 11:02
     * @功能说明:编辑员工每一天的排班情况
     */
    public function updateClassData($input,$uniacid){

        $connect_model = new ClassConnect();

        $class_model   = new ResClass();

        foreach ($input['time'] as $vs){

            if(empty($vs['class_id'])){

                continue;
            }
            //确保是0点
            $vs['time'] = strtotime(date('Y-m-d',$vs['time']));

            $where = [

                'uniacid' => $uniacid,

                'date'    => $vs['time'],

                'staff_id'=> $input['staff_id'],

                'store_id'=> $input['store_id']

            ];
            //查询排班情况
            $data  = $this->dataInfo($where);
            //查询一下班次
            $class = $class_model->dataInfo(['id'=>$vs['class_id']]);

            if(empty($class)){

                return ['code'=>500,'msg'=>'班次不存在'];

            }
            //处理好的时间戳
            $date_str = $this->changeTime($vs['time'],$class['start_time'],$class['end_time']);

            $insert = [

                'uniacid'    => $uniacid,

                'store_id'   => $input['store_id'],

                'date'       => $vs['time'],

                'date_text'  => date('Y-m-d',$vs['time']),

                'staff_id'   => $input['staff_id'],

                'start_time' => $date_str['start_time'],

                'end_time'   => $date_str['end_time'],

                'class_id'   => $class['id'],

                'class_title'=> $class['title'],
            ];

            Db::startTrans();
            //没有则添加
            if(empty($data)){

                $res = $this->dataAdd($insert);

                if($res!=1){

                    Db::rollback();

                    return ['code'=>500,'msg'=>'添加失败'];
                }

                $date_id = $this->getLastInsID();

            }else{

                $res = $this->dataUpdate(['id'=>$data['id']],$insert);

                $date_id = $data['id'];

            }
            //添加桌号绑定
            $connect_model->where(['date_id'=>$date_id])->delete();

            if($class['type']==1){

                foreach ($input['table_id'] as $value){
                    //删除改天该班次该桌号的信息
                    $this->delDateTable($input['store_id'],$vs['time'],$class['id'],$value);

                    $insert = [

                        'uniacid' => $uniacid,

                        'date_id' => $date_id,

                        'table_id'=> $value

                    ];

                    $res = $connect_model->dataAdd($insert);

                    if($res!=1){

                        Db::rollback();

                        return ['code'=>500,'msg'=>'添加失败'];

                    }

                }

            }
            //删除没有桌号的排班
            $this->delNullTable($input['store_id'],$vs['time'],$class['id']);

            Db::commit();

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-12 11:55
     * @功能说明:删除没有桌号的排班
     */
    public function delNullTable($store_id,$date,$class_id){

        $dis = [

            'store_id' => $store_id,

            'date'     => $date,

            'class_id' => $class_id,

        ];

        $list = $this->where($dis)->select()->toArray();

        if(!empty($list)){

            $class_model  = new ResClass();

            $connect_model = new ClassConnect();

            foreach ($list as $value){

                $class = $class_model->dataInfo(['id'=>$value['class_id']]);

                if(!empty($class)&&$class['type']==1){

                    $date = $connect_model->dataInfo(['date_id'=>$value['id']]);

                    if(empty($date)){

                        $this->where(['id'=>$value])->delete();

                    }

                }

            }

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-12 10:48
     * @功能说明:删除当天某一个班次下面的桌号
     */
    public function delDateTable($store_id,$date,$class_id,$table_id){

        $dis = [

            'store_id' => $store_id,

            'date'     => $date,

            'class_id' => $class_id,

        ];

        $id  = $this->where($dis)->column('id');

        $connect_model = new ClassConnect();

        if(!empty($id)){

            $connect_model->where('date_id','in',$id)->where(['table_id'=>$table_id])->delete();

        }

        return true;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-01 10:08
     * @功能说明:删除该商家某一天的排班
     */

    public function delDateClass($store_id,$date,$class_id){

        $dis = [

            'store_id' => $store_id,

            'date'     => $date,

            'class_id' => $class_id

        ];

        $id  = $this->where($dis)->column('id');

        if(!empty($id)){

            $connect_model = new ClassConnect();

            $connect_model->where('date_id','in',$id)->delete();

        }

        $res = $this->where($dis)->delete();

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-02 09:21
     * @功能说明:获取开始结束时间
     */
    public function changeTime($date,$start_time,$end_time){

        $to_day = strtotime(date('Y-m-d',time()));

        $end_time   = $end_time>$start_time? strtotime($end_time):strtotime($end_time)+86400;

        $start_time = strtotime($start_time);

        $data['start_time'] = $start_time+$date-$to_day;

        $data['end_time']   = $end_time+$date-$to_day;

        return $data;

    }





    /**
     * @author chenniang
     * @DataTime: 2021-03-02 09:54
     * @功能说明:通过当前桌号查看员工
     */
    public function tableFindStaff($table_id){

        $table_model = new Table();

        $table = $table_model->dataInfo(['id'=>$table_id,'status'=>1]);

        if(empty($table)){

            return ['code'=>500,'msg'=>'当前桌位已下架'];

        }

        $store_id = $table['store_id'];

        $dis[] = ['b.table_id','=',$table_id];

        $dis[] = ['a.store_id','=',$store_id];

        $dis[] = ['a.start_time','<',time()];

        $dis[] = ['a.end_time','>',time()];

        $dis[] = ['c.type','=',1];

        $staff_id = $this->alias('a')
                    ->join('longbing_card_restaurant_class_connect b','a.id = b.date_id')
                    ->join('longbing_card_restaurant_class c','c.id = a.class_id')
                    ->where($dis)
                    ->value('a.staff_id');
        //如果没有绑定的员工 就指派到管理员
        if(empty($staff_id)){

            $staff_model = new StoreStaff();

            $staff_id = $staff_model->getStaffId($store_id);

        }

        if(empty($staff_id)){

            return ['code'=>500,'msg'=>'当前桌号未绑定员工'];
        }

        $info['store_id'] = $store_id;

        $info['staff_id'] = $staff_id;

        return $info;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-02 15:55
     * @功能说明:桌号绑定的员工
     */
    public function bindTableStaff($dis){

        $staff_id = $this->alias('a')
            ->join('longbing_card_restaurant_class_connect b','a.id = b.date_id')
            ->join('longbing_card_user_info c','c.fans_id = a.staff_id','left')
            ->where($dis)
            ->field('a.staff_id,c.name')
            ->find();

        return !empty($staff_id)?$staff_id->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-02 11:52
     * @功能说明:查看某一天桌号排班情况
     */
    public function tableStaffList($dis){

        $table_model = new Table();

        $data = $table_model->alias('a')
                ->join('longbing_card_restaurant_class_connect b','a.id = b.table_id')
                ->join('longbing_card_restaurant_class_date c','b.date_id = c.id')
                ->join('longbing_card_user_info d','d.fans_id = c.staff_id')
                ->where($dis)
                ->field('a.title,d.name,c.staff_id')
                ->group('a.id')
                ->paginate(10)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $v['name'] = !empty($v['name'])?$v['name']:lbUserName($v['staff_id']);

            }
        }

        return $data;
    }








}